package com.yenlien.traceyun.milk.service;

import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import com.yenlien.traceyun.milk.dao.MilkDetectionDao;
import com.yenlien.traceyun.milk.entity.MilkDetection;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date 2022/7/11
 * @Author jiaLong
 * @Descript
 */
@Service
public class MilkDetectionService {

    @Autowired
    private MilkDetectionDao detectionDao;

    @Autowired
    private FileRestService fileRestService;

    public List<MilkDetection> findAll(){
        return detectionDao.findAll();
    }


    public MilkDetection save(MilkDetection detection, List<MultipartFile> files){
        detection.setBatch((LocalDate.now() + RandomStringUtils.randomNumeric(8)).replace("-", ""));
        detection.setDetectTime(new Date());
        detection.setDetectPhoto(fileRestService.upload(files));
        return detectionDao.save(detection);
    }



    public Object findByList(MilkDetection detection) {
        if("1".equals(detection.getType())){
            Pageable pageable = PageRequest.of(detection.getPage(), detection.getPageSize(), Sort.Direction.DESC, "createtime");
            return detectionDao.findAll((Root<MilkDetection> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                List<Predicate> list = new ArrayList<>();
                if (!StringUtils.isEmpty(detection.getCompanyId())) {
                    list.add(cb.equal(root.get("companyId"), detection.getCompanyId()));
                }
                if (!StringUtils.isEmpty(detection.getIdentifier())) {
                    list.add(cb.like(root.get("identifier"), "%" + detection.getIdentifier() + "%"));
                }
                if (!StringUtils.isEmpty(detection.getFarmersName())) {
                    list.add(cb.like(root.get("farmersName"), "%" + detection.getFarmersName() + "%"));
                }
                if (!StringUtils.isEmpty(detection.getPastureName())) {
                    list.add(cb.like(root.get("pastureName"), "%" + detection.getPastureName() + "%"));
                }
                if (detection.getStartTime() != null && detection.getEndTime() != null) {
                    list.add(cb.between(root.get("detectTime"), detection.getStartTime(), detection.getEndTime()));
                }
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }, pageable);
        }else {
            // 成品 LEFT JOIN decetion

            return null;
        }
    }




}
